package cn.bailan.controller.restController;
import cn.bailan.entity.Constant;
import cn.bailan.entity.MoralBalance;
import cn.bailan.entity.Page;
import cn.bailan.entity.R.R;
import cn.bailan.entity.User;
import cn.bailan.service.IUserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

/**
 * Author:甲粒子
 * Date: 2022/6/4
 * Description：
 */
@RestController
@Api(value = "USERCONTROLLER")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "微信登录", notes = "微信登录")
    @ResponseBody
    @PostMapping(value = "/api/user/wxLogin",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object wxLogin(@RequestBody String data, HttpServletRequest request) throws JsonProcessingException {
        return userService.wxLogin(data,request);
    }



    @ApiOperation(value = "更新用户头像", notes = "更新用户头像")
    @ResponseBody
    @PostMapping(value = "/api/user/updateAvatar/{userId}/{avatar}",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object updateAvatar(@PathVariable Integer userId,@PathVariable String avatar){
        try{
            userService.setAvatar(userId,avatar);
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }

    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    @ResponseBody
    @PostMapping(value = "/api/user/updateUser",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object updateUserInfo(@RequestBody User user){
        try{
            userService.updateUserInfo(user);
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }

    @ApiOperation(value = "获取卷王排行前25", notes = "获取卷王排行前25")
    @ResponseBody
    @GetMapping(value = "/api/user/getTop25Good",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object getTop25Good(){
        try{
            return R.ok().data("data", userService.getTop25Good().stream()
                    .filter(user -> {
                        return user.getScore() >= 50;
                    })
                    .sorted((u1,u2)->{
                return (int)( u2.getScore() - u1.getScore());
            }));
        }catch (Exception e){
            return R.error();
        }
    }

    @ApiOperation(value = "获取摆王排行前25", notes = "获取摆王排行前25")
    @ResponseBody
    @GetMapping(value = "/api/user/getTop25Bad",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object getTop25Bad(){
        try{
            return R.ok().data("data", userService.getTop25Bad().stream()
                    .filter(user -> {
                        return user.getScore() < 0;
                    })
                    .sorted((u1,u2)->{
               return (int)( u1.getScore() - u2.getScore());
            }));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "获取用户列表(curPage从0开始)", notes = "获取用户列表")
    @ResponseBody
    @GetMapping(value = "/api/user/getUserList/{curPage}",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object getUserList(@PathVariable long curPage){
        try{
            Page<User> page = new Page<>();
            page.setCurrentpage(curPage);
            page.setTotalcount(userService.countUser());
            page.setPagesize(Constant.PAGE_SIZE);
            page.setData(userService.getUserList(curPage* Constant.PAGE_SIZE,Constant.PAGE_SIZE)
                    .stream()
                    .filter(user -> {
                        return user.getScore() >= 0 && user.getScore() < 50;
                    })
                    .sorted((u1,u2)->{
                        return u2.getRTime().compareTo(u1.getRTime());
                    }).collect(Collectors.toList())
            );
            return R.ok().data("data", page);
        }catch (Exception e){
            return R.error();
        }
    }


    @ApiOperation(value = "获取激励语言一句", notes = "获取激励语言一句")
    @ResponseBody
    @GetMapping(value = "/api/user/getInspiration",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object getInspiration(){
        try{
            //String inspiration = userService.getInspiration();
            //System.out.println(inspiration);
            return R.ok().data("data",userService.getInspiration());
        }catch (Exception e){
            return R.error();
        }
    }


    @ApiOperation(value = "用户提交今日道德余额(id不用填，UserId等要填，bad分数未负数，score为总分数)", notes = "用户提交今日道德余额")
    @ResponseBody
    @PostMapping(value = "/api/user/submitToday",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object submitToday(@RequestBody MoralBalance moralBalance){
        try{
            userService.addMoralBalance(moralBalance);
            userService.updateScore(moralBalance.getUserId(),moralBalance.getScore());
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }


    @ApiOperation(value = "获取道德余额列表(curPage从1开始)", notes = "获取道德余额列表")
    @ResponseBody
    @GetMapping(value = "/api/user/getMoralBalanceList/{curPage}/{userId}",produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Object getMoralBalanceList(@PathVariable long curPage,@PathVariable Integer userId){
        try{
            Page<MoralBalance> page = new Page<>();
            page.setCurrentpage(curPage);
            page.setTotalcount(userService.countMoralBalance(userId));
            page.setPagesize(Constant.PAGE_SIZE);
            page.setData(userService.getMoralBalanceList(userId,curPage,Constant.PAGE_SIZE));
            return R.ok().data("data", page);
        }catch (Exception e){
            return R.error();
        }
    }
}
